﻿/* $RCSFile: AddUsersDownloads.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ECommerce/PageModules/Download/AddUsersDownloads.aspx.cs $
 * $log$
 * Revision 21 2010/08/09 11:08:11  christian.surieux
 *   Added Log comment to all .ascx .aspx .master files
 * Revision 8 2010/08/09 10:07:05  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Globalization;

using AddonNice.Helpers;
using AddonNice.Security;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.ECommerce;
using AddonNice.Settings.Download;

using AddonNice.CRMWrapper.ECommerce;
using AddonNice.Settings.ECommerce;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.Download
{
    public partial class AddUsersDownloads : AddEditItemPage
    {
        Prod2DownLoadDB dwnDB   =   new Prod2DownLoadDB();

        int WarrantyPeriod
        {
            get
            {
                return moduleSettingsDic[EComStStr.WarrantyPeriod].ToInt();
            }
        }

        public AddUsersDownloads()
            : base(false, true, true, false)
        { }

        protected void Page_Load(object sender, System.EventArgs e) 
        {
            if (ModuleTraceSwitch.Sw.TraceError)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]AddUsersDownloads Page_Load IsPostBack: {0}.",IsPostBack),ModuleTraceSwitch.Sw.Info);
            if (!IsPostBack)
            {
                BindUsers();
                BindProducts();
                RadStartDate.SelectedDate   =   DateTime.Now;
                RadEndDate.SelectedDate     =   DateTime.Now.AddMonths(WarrantyPeriod);
            }
        }

        void BindUsers()
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]AddUsersDownloads BindUsers BEG.",ModuleTraceSwitch.Sw.Info);
            // fill combobox with users
            MembershipUserCollection   tbUsers  =   null;
            try
            {
                tbUsers                 =   Membership.GetAllUsers();
                CbUserId.DataSource     =   tbUsers;
                CbUserId.DataTextField  =   "UserName";
                CbUserId.DataValueField =   "UserId";
                CbUserId.DataBind();
            }
            catch (Exception ex)
            {
                if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]AddUsersDownloads BindUsers ex: {0}.",ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
        }

        void BindProducts()
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]AddUsersDownloads BindProducts BEG.",ModuleTraceSwitch.Sw.Info);
            SqlDataReader dr            =   null;
            try
            {  
                dr                      =   dwnDB.GetAllProd2DownLoadForBuyers();
                CbProductId.Items.Clear();
                while( dr.Read() )
                {
                    Guid ProductId      =   (Guid)dr["ProductId"];
                    ADNDynEProduct prod =   new ProductsCRM().GetProduct(ProductId);
                    if (prod != null)
                        CbProductId.Items.Add(new RadComboBoxItem(prod.name,ProductId.ToString()));
                    }
            }
            catch(Exception ex)
            { 
                if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]AddUsersDownloads BindProducts ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
            finally
            {
                if ( dr != null )
                    dr.Close();
            }
        }

        /// <summary>
		/// Set the module guids with free access to this page
		/// </summary>
		protected override ArrayList AllowedModules
		{
			get
			{
				ArrayList al    =   new ArrayList();
                char[] aa       =   new char[]{'}','{'};
				al.Add (EComStStr.UsersDownLoadsGuid.Trim(aa));
				return al;
			}
		}

        void AddUsersDownloads_Update(object sender, AddonNice.UI.ADNPageEventArgs e)
        {
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]AddUsersDownloads AddUsersDownloads_Update BEG.",PageTraceSwitch.Sw.Info);
            try
            {
                Guid ProductId              =   new Guid(CbProductId.SelectedValue);
                DateTime StartDate          =   RadStartDate.SelectedDate.GetValueOrDefault(DateTime.Now);
                DateTime EndDate            =   RadEndDate.SelectedDate.GetValueOrDefault(DateTime.Now);
                int NbDownload              =   0;
                Guid UserId                 =   new Guid(CbUserId.SelectedValue);
		        if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]AddUsersDownloads AddUsersDownloads_Update UserId: {0}, ProductId: {1},StartDate: {2}, StartDate: {3}",
                        new object[]{UserId,ProductId,StartDate,EndDate}),ModuleTraceSwitch.Sw.Info);
                int UserDownloadId          =   dwnDB.AddUserDownLoad(UserId,ProductId,StartDate,EndDate,NbDownload);
		        if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]AddUsersDownloads AddUsersDownloads_Update END UserDownloadId: {0}",UserDownloadId),ModuleTraceSwitch.Sw.Info);
            }
            catch (Exception ex)
            {
			    if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]AddUsersDownloads RadGrid1_UpdateCommand ex: {0}",ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
        }
    
		#region Std overrides
        
        /// <summary>
        /// Here we create the command buttons and add them to the PlaceHolders prepared
        /// </summary>
        override protected void OnPreInit(EventArgs e)
        {
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]AddUsersDownloads OnPreInit BEG.",PageTraceSwitch.Sw.Info);
            base.OnPreInit(e);
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]AddUsersDownloads OnPreInit END.",PageTraceSwitch.Sw.Info);
        }

        override protected void OnInit(EventArgs e)
        {
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]AddUsersDownloads Init BEG.",PageTraceSwitch.Sw.Info);
            InitializeComponent();
		
            base.OnInit(e);
			if (PageTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( "[V]AddUsersDownloads Init End",PageTraceSwitch.Sw.Info);
        }

        private void InitializeComponent() 
        {
			Load	    +=  new EventHandler(Page_Load);
            Update      +=  new PageEventHandler(AddUsersDownloads_Update);
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons
		{
			get
			{ return PlaceHolderButtons; }
			set { }
		}

        protected override System.Web.UI.WebControls.PlaceHolder placeHolderForButtons2
		{
			get
			{ return PlaceHolderButtons2; }
			set { }
		}
        
        #endregion
    }
}
